<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" ref="chartsRef" />
</template>
<script lang="ts" setup>
import * as echarts from 'echarts'
import { onMounted, ref } from 'vue'
const chartsRef = ref<HTMLElement | null>()

const props = defineProps({
  className: {
    type: String,
    default: 'chart',
  },
  xAxisData: {
    type: Array,
    default: () => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  },
  config: {
    type: Object,
    default: () => {},
  },
  seriesData: {
    type: Array,
    default: () => [150, 230, 224, 218, 135, 147, 260],
  },
  id: {
    type: String,
    default: 'chart',
  },
  width: {
    type: String,
    default: '200px',
  },
  height: {
    type: String,
    default: '200px',
  },
})
const options = {
  grid: {
    left: '2%',
    right: '2%',
    bottom: '2%',
    containLabel: true, //grid 区域是否包含坐标轴的刻度标签
  },
  tooltip: {
    //提示框组件
    trigger: 'axis', //触发类型
  },
  legend: {
    data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'],
  },
  xAxis: {
    type: 'category',
    axisLine: {}, //坐标轴轴线相关设置
    axisTick: {}, //坐标轴刻度相关设置
    boundaryGap: false, //坐标轴两边留白策略
    splitLine: {}, //坐标轴在 grid 区域中的分隔线
    data: props.xAxisData,
  },
  yAxis: {
    type: 'value',
  },
  series: [
    {
      name: 'Email',
      data: props.seriesData,
      type: 'line',
      smooth: true, //是否平滑曲线显示
      showSymbol: false, //是否显示 symbol
    },
    {
      name: 'Union Ads',
      type: 'line',
      stack: 'Total',
      smooth: true,
      showSymbol: false,
      data: [220, 182, 191, 234, 290, 330, 310],
    },
    {
      name: 'Video Ads',
      type: 'line',
      stack: 'Total',
      smooth: true,
      showSymbol: false,
      data: [150, 232, 201, 154, 190, 330, 410],
    },
    {
      name: 'Direct',
      type: 'line',
      stack: 'Total',
      smooth: true,
      showSymbol: false,
      data: [320, 332, 301, 334, 390, 330, 320],
    },
    {
      name: 'Search Engine',
      type: 'line',
      stack: 'Total',
      smooth: true,
      showSymbol: false,
      data: [820, 932, 901, 934, 1290, 1330, 1320],
    },
  ],
  ...props.config,
}
const initChart = (val) => {
  const chart = echarts.init(chartsRef.value)
  chart.setOption(options)
}
onMounted(() => {
  window.addEventListener('resize', function () {
    initChart()
  })
})

defineExpose({
  initChart,
})
</script>
 
<style></style>